package com.banciyuan.bcywebview.biz.apprecommend;

import android.content.Context;
import android.support.annotation.BoolRes;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.AcgItem;

/**
 * Created by hy on 2015/7/7.
 */
public class RecommendAcgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<AcgItem> data = new ArrayList<>();
    private Context context;
    private ImageLoader imageLoader;
    private Map<String, String> likeMaps = new HashMap<>();
    private RecommendAcgActivity.CheckSelect checkSelect;


    public RecommendAcgAdapter(Context context, List<AcgItem> data, RecommendAcgActivity.CheckSelect checkSelect) {
        this.context = context;
        this.data = data;
        this.checkSelect = checkSelect;
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return new ViewHeader(View.inflate(context, R.layout.hotacg_header, null));
            case 1:
                return new ViewWork(View.inflate(context, R.layout.acg_hot_item, null));
            default:
            case 2:
                return new Viewfooter(View.inflate(context, R.layout.smooth_blank, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 1:
                renderData((ViewWork) holder, data.get(position - 1));
                break;
            case 2:
                Viewfooter viewfooter = (Viewfooter) holder;
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, UIUtils.dip2px(140, context));
                viewfooter.blank.setLayoutParams(layoutParams);
                break;
            case 0:
                break;
        }
    }

    private void renderData(ViewWork viewWork, final AcgItem mItem) {
        if (StringUtil.notNullStartWithUrlHead(mItem.getImg_src())) {
            imageLoader.displayImage(mItem.getImg_src(), viewWork.ivContent, ImageOptions.options);
        } else {
            viewWork.ivContent.setImageResource(R.drawable.acg_default_bg);
        }
        viewWork.tvRealName.setText(mItem.getReal_name());
        if (mItem.isWf_status()) {
            viewWork.ivChecked.setVisibility(View.VISIBLE);
            viewWork.viewBack.setBackgroundColor(context.getResources().getColor(R.color.cc_black));
        } else {
            viewWork.ivChecked.setVisibility(View.GONE);
            viewWork.viewBack.setBackgroundColor(context.getResources().getColor(R.color.four_c_black));
        }
        viewWork.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doLikeInMemory(mItem);
                checkItem(mItem, view.findViewById(R.id.view_bg), view.findViewById(R.id.iv_acg_checked));
                checkSelect.check();
            }
        });
    }

    private void checkItem(AcgItem mItem, View view, View check) {
        if (mItem.isWf_status()) {
            check.setVisibility(View.VISIBLE);
            view.setBackgroundColor(context.getResources().getColor(R.color.cc_black));
        } else {
            check.setVisibility(View.GONE);
            view.setBackgroundColor(context.getResources().getColor(R.color.four_c_black));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (position == data.size() + 1) {
            return 2;
        } else {
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        return data.size() + 2;
    }

    private class ViewWork extends RecyclerView.ViewHolder {
        ImageView ivContent;
        TextView tvRealName;
        ImageView ivChecked;
        View viewBack;

        public ViewWork(View itemView) {
            super(itemView);
            ivContent = (ImageView) itemView.findViewById(R.id.iv_content);
            tvRealName = (TextView) itemView.findViewById(R.id.tv_realname);
            ivChecked = (ImageView) itemView.findViewById(R.id.iv_acg_checked);
            viewBack = itemView.findViewById(R.id.view_bg);
        }
    }

    private class ViewHeader extends RecyclerView.ViewHolder {

        public ViewHeader(View itemView) {
            super(itemView);
        }
    }

    private class Viewfooter extends RecyclerView.ViewHolder {

        View blank;

        public Viewfooter(View itemView) {
            super(itemView);
            blank = itemView.findViewById(R.id.blank);
        }
    }


    private void doLikeInMemory(AcgItem acgItem) {
        if (acgItem.isWf_status()) {
            acgItem.setWf_status(false);
            likeMaps.put(acgItem.getId(), "unfollow");
        } else {
            acgItem.setWf_status(true);
            likeMaps.put(acgItem.getId(), "follow");
        }
    }

    public Map<String, String> getLikeMaps() {
        return likeMaps;
    }

}
